﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;

public partial class User_dang_ky : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserEmail"] == null & Request.Cookies["UserEmail"] == null)
            Response.Redirect("~/User/dang-nhap.aspx");
        if (Session["UserEmail"] == null & Request.Cookies["UserEmail"] != null)
            if (Request.Cookies["UserEmail"].Value == "")
                Response.Redirect("~/User/dang-nhap.aspx");
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetUserAvatar";
        Dal.ParameterCollection = new string[1] { "@UserEmail" };
        if (Session["UserEmail"] != null)
            Dal.ValueCollection = new string[1] { Session["UserEmail"].ToString() };
        if (Session["UserEmail"] == null & Request.Cookies["UserEmail"] != null)
            if (Request.Cookies["UserEmail"].Value != "")
                Dal.ValueCollection = new string[1] { Request.Cookies["UserEmail"].ToString() };
        DataRow dr = Dal.GetDataRow();
        avatar.ImageUrl = "~/Images/UserAvatar/" + dr["Image"].ToString();

    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string password1 = Md5(txtOldPassword.Text);
        string password2 = Md5(txtNewPassword.Text);
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_ChangePassword";
            Dal.ParameterCollection = new string[3] { "@UserEmail","@MatKhauCu", "@MatKhauMoi" };
            Dal.ValueCollection = new string[3] { txtEmail.Text,password1,password2 };
            Dal.ReturnValueParameter = "@ReturnValue";
            Dal.ExecuteNonQuery();
            if (Dal.ReturnValue == 1)
                lblInfo.Text = "Đổi mật khẩu thành công";
            if (Dal.ReturnValue == 2)
                lblInfo.Text = "Sai mật khẩu";
            if (Dal.ReturnValue == 0)
                lblInfo.Text = "Email không tồn tại";
        }
        catch
        { 
            
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        clear();
    }
    private void clear()
    {
        txtNewPassword.Text = "";
        txtEmail.Text = "";
        txtOldPassword.Text = "";
        txtReNewPassword.Text = "";
        lblInfo.Text = "";
    }
    public static string Md5(string strPass)
    {
        try
        {
            MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(strPass));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
        catch (Exception objEx)
        {
        }
        return string.Empty;
    }
}
